
// 被代理的对象
const obj = {
    name: '_island',
    score: 95
}

// const objProxy = new Proxy(obj, {});
// console.log(objProxy);

const objProxy = new Proxy(obj, {
    // handler 处理捕捉器
    get: function (target, key) {
        console.log(`捕获到对象获取${key}属性的值操作`);
        return target[key];
    },
    // 写操作
    // 对象的访问拦截,捕捉器set
    set: function (target, key, val) {
        if (key == 'age') {
            if (typeof val == 'number') {
                console.log(`捕获到对象操作${key}属性的值操作`);
                target[key] = val;
            }
            else {
                throw new TypeError('该属性值必须是Number类型');
                // console.warn('该属性值必须是Number类型');
            }
        }
        else {
            target[key] = val;
        }
    }
})

console.log(objProxy.name);
try {
    // objProxy.age = '999';
    objProxy.age = 11;
}
catch (e) {
    console.log('error');
}
// objProxy.age = '999';
console.log(objProxy.age);
